package com.ficus.road.maintain.service.smartnew;

import com.ficus.road.maintain.core.model.smart.RoadDiseaseCount;
import com.ficus.road.maintain.core.model.smartnew.SmartScreening;
import com.ficus.road.maintain.dao.smartnew.SmartScreeningDao;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/3/1 下午2:13
 */
@Service
@RequiredArgsConstructor
public class SmartScreeningService {
    private final SmartScreeningDao smartScreeningDao;
//    private final HitDao hitDao;

    public void create(SmartScreening smartScreening) {
        smartScreeningDao.saveOrUpdate(smartScreening);
    }

    public void deleteAll() {
        smartScreeningDao.deleteAll();
    }

    /**
     * 年度统计
     *
     * @return
     */
    public List<SmartScreening> statisticAnnually(Query query) {
        int year = MyDateUtil.getYear(new Date());
        query.addCriteria(Criteria.where("creation_time").gte(MyDateUtil.strParseDate(year + "-01-01", "yyyy-MM-dd").getTime() / 1000));
        query.with(new Sort(Sort.Direction.DESC, "count"));
        List<SmartScreening> smartScreenings = smartScreeningDao.findByQuery(query);
        return smartScreenings;
    }

    public RoadDiseaseCount convert2RoadDiseaseCount(SmartScreening smartScreening) {
        return RoadDiseaseCount.builder().count(smartScreening.getCount())
                .road(smartScreening.getRoadName())
                .id(smartScreening.getId())
                .build();
    }

    public List<SmartScreening> findByQuery(Query query) {
        return smartScreeningDao.findByQuery(query);
    }
}
